﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp
{
    /// <summary>
    /// Interaction logic for Overview.xaml
    /// </summary>
    public partial class ErrorProviderDemo : Window, IWindow
    {
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
        }
  
        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        }

        public void ClearProvider()
        {
            this.provider.Clear();
        }

        private ErrorProvider provider = new ErrorProvider();

        public ErrorProviderDemo()
        {
           
            InitializeComponent();
            this.Background = new SolidColorBrush(Colors.Black);

            this.NumberEditor.Minimum = Int64.Parse(this.Minimum.Text);
            this.NumberEditor.Maximum = Int64.Parse(this.Maximum.Text);
            this.NumberEditor.Value = 0;

            this.Alignment.Items.Add("TopLeft");
            this.Alignment.Items.Add("TopRight");
            this.Alignment.Items.Add("MiddleLeft");
            this.Alignment.Items.Add("MiddleRight");
            this.Alignment.Items.Add("BottomLeft");
            this.Alignment.Items.Add("BottomRight");
            this.Alignment.SelectionChanged += new SelectionChangedEventHandler(Alignment_SelectionChanged);

            this.NumberEditor.ValueChanged += new VIBlend.WPF.Controls.ValueChangedEditorEventHandler(NumberEditor_ValueChanged);

            this.Minimum.TextChanged += new TextChangedEventHandler(Minimum_TextChanged);
            this.Maximum.TextChanged += new TextChangedEventHandler(Maximum_TextChanged);
            this.LayoutRoot.Children.Add(provider);

            this.MinHeight = 520;
            this.MinWidth = 750;
        }

        void Maximum_TextChanged(object sender, TextChangedEventArgs e)
        {
            long result;
            if (Int64.TryParse(this.Maximum.Text, out result))
            {
                this.NumberEditor.Maximum = result;
            }
            else
            {
                this.Maximum.Text = "10000";
            }
        }

        void Minimum_TextChanged(object sender, TextChangedEventArgs e)
        {
            long result;
            if (Int64.TryParse(this.Minimum.Text, out result))
            {
                this.NumberEditor.Minimum = result;
            }
            else
            {
                this.Minimum.Text = "-10000";
            }
        }

        void NumberEditor_ValueChanged(object sender, VIBlend.WPF.Controls.ValueChangedEditorEventArgs args)
        {
            if (args.Value < this.NumberEditor.Minimum)
            {
                this.provider.SetError(this.NumberEditor, "Value is less than the Minimum value");
            }
            else if (args.Value > this.NumberEditor.Maximum)
            {
                this.provider.SetError(this.NumberEditor, "Value is greater than the Maximum value!");
            }
            else this.provider.SetError(this.NumberEditor, "");


        }

        void Alignment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.Alignment.SelectedIndex)
            {
                case 0:
                    provider.IconAlignment = ErrorProviderIconAlignment.TopLeft;
                    break;
                case 1:
                    provider.IconAlignment = ErrorProviderIconAlignment.TopRight;
                    break;
                case 2:
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleLeft;
                    break;
                case 3:
                    provider.IconAlignment = ErrorProviderIconAlignment.MiddleRight;
                    break;
                case 4:
                    provider.IconAlignment = ErrorProviderIconAlignment.BottomLeft;
                    break;
                case 5:
                    provider.IconAlignment = ErrorProviderIconAlignment.BottomRight;
                    break;

            }
        }


        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {


        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.BlinkIfDifferent == null)
                return;

            if (this.BlinkIfDifferent.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.BlinkIfDifferentError;
            }
            else if (this.NeverBlink.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.NeverBlink;
            }
            else if (this.AlwaysBlink.IsChecked.Value)
            {
                this.provider.BlinkStyle = ErrorProviderBlinkStyle.AlwaysBlink;
            }
        }
    }
}
